home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / mystic.zip / PAS6.DOC < prev    next >
Text File  |  1986-02-27  |  16KB  |  914 lines

  1.  
  2.         Mystic Pascal  User Manual                                      59
  3.  
  4.  
  5.         9.  Input/Output
  6.  
  7.              Mystic  Pascal  supports all standard Pascal I/O  procedures 
  8.         and  functions  and has extensions to support  random  disk  file 
  9.         access and to assign a DOS file name to a Pascal file variable.
  10.  
  11.         +    ASSIGN         associate a file name with a file variable
  12.         +    CLOSE          terminate processing on a file 
  13.              EOF            Boolean function indicates end-of-file 
  14.                             condition
  15.              EOLN           Boolean function indicates end-of-line
  16.                             condition for text files
  17.         1.6  GET            move file pointer to next component
  18.         +    IORESULT       integer function status of I/O operation
  19.              PAGE           advance textfile output to new page
  20.         1.6  PUT            append buffer variable to file
  21.              READ           obtain input from console or disk file
  22.              READLN         obtain textfile input from new line
  23.              RESET          prepare an existing file for input
  24.              REWRITE        create a file for output
  25.         +    SEEK           position the file pointer for random access
  26.              WRITE          output data to console, printer, disk file
  27.              WRITELN        output textfile data and terminate line
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.         Section 9:   Input/Output
  58.  
  59.         Mystic Pascal  User Manual                                      60
  60.  
  61.  
  62.         9.1  File Variables
  63.  
  64.              File variables,  like other variables, are declared in a VAR 
  65.         section.  They indicated the data type of the file's components.
  66.  
  67.              TYPE
  68.              DATAFILE = FILE OF REAL;
  69.              VAR
  70.              F1 : DATAFILE;
  71.              F2 : FILE OF ARRAY [0..63] OF CHAR;
  72.              F3 : TEXT;
  73.  
  74.              Textfiles   are  declared  by  the  predefined  type   TEXT.  
  75.         Textfiles consist of lines of characters separated by the end-of-
  76.         line byte sequence 0DH, 0AH (carriage return, line feed).
  77.  
  78.  
  79.  
  80.         9.2  Standard Files
  81.  
  82.              The  DOS operating system supports several standard  "files" 
  83.         or logical devices which are always available.  These may be used 
  84.         directly  as  file variables in Read/ln and Write/ln.   They  may 
  85.         also  be  assigned  to  Pascal  file  variables  by  the   Assign 
  86.         procedure.
  87.  
  88.              CON       standard console device  (input/output)
  89.              KBD       keyboard device  (input without echoing)
  90.              AUX       auxiliary device  (input/output)
  91.              LST       printer  (output)
  92.              PRN       printer  (output)
  93.  
  94.              If  no file variable is specified as the first parameter  in 
  95.         the  Read/ln or Write/ln procedures,  the default files INPUT and 
  96.         OUTPUT respectively are used.  These are assigned to the standard 
  97.         console and keyboard devices.
  98.  
  99.  
  100.  
  101.         9.3  How to Send Data to Your Printer
  102.  
  103.              To  route  output to your printer specify the Standard  File 
  104.         LST or PRN in your WRITE or WRITELN statements. 
  105.  
  106.              WRITELN( PRN, 'This line will be printed.');
  107.  
  108.              WRITE( PRN, 'X =',X,'   Y =',Y,'   Z =',Z );
  109.  
  110.  
  111.  
  112.  
  113.  
  114.         Section 9:   Input/Output
  115.  
  116.         Mystic Pascal  User Manual                                      61
  117.  
  118.  
  119.         9.4  ASSIGN        (Non-Standard Feature)
  120.  
  121.              ASSIGN( Filvar, String )
  122.  
  123.              ASSIGN( Filvar, Standard_file )
  124.  
  125.              The  procedure  Assign is used to associate a file  variable 
  126.         with a particular disk file or standard file.
  127.  
  128.              The  ability  to  assign standard files to a  file  variable 
  129.         allows  complete I/O redirection within a  Pascal  program.   For 
  130.         example,  a  program which creates reports could route its output 
  131.         to a disk file one time and to a printer or console another time.  
  132.         There  is no need for separate WRITE statements for each type  of 
  133.         output.
  134.  
  135.              10: WRITELN('Select output device for Report Listing');
  136.                  WRITELN('1 = Console   2 = Printer   3 = Diskfile');
  137.              READLN(X);
  138.              CASE X OF
  139.              1 : ASSIGN( REPORTFILE, CON );
  140.              2 : ASSIGN( REPORTFILE, PRN );
  141.              3 : ASSIGN( REPORTFILE, 'REPORT.LST' );
  142.              ELSE GOTO 10
  143.              END; 
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.         Section 9:   Input/Output
  172.  
  173.         Mystic Pascal  User Manual                                      62
  174.  
  175.  
  176.         9.5  CLOSE         (Non-Standard Feature)
  177.  
  178.         CLOSE( Filvar )
  179.  
  180.              The   Close  procedure  must  be  called  after   completing 
  181.         processing  on  a disk file to ensure that the disk directory  is 
  182.         updated.  Failure to Close a file after updating it may result in 
  183.         the loss of data.
  184.  
  185.              Closing  a file variable to which a standard file  has  been 
  186.         assigned is treated as a null operation.
  187.  
  188.  
  189.              CLOSE( F1 );
  190.  
  191.              CLOSE( REPORTFILE );
  192.  
  193.              CLOSE( SORTFILE2 );
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.         Section 9:   Input/Output
  229.  
  230.         Mystic Pascal  User Manual                                      63
  231.  
  232.  
  233.         9.6  EOF
  234.  
  235.         EOF( Filvar )
  236.  
  237.              Eof  is  a Boolean function which indicates the  end-of-file 
  238.         condition.   Eof  is true only when the file pointer points  past 
  239.         the last component of the file, otherwise it is false.
  240.  
  241.  
  242.              { Copy F1 into F2 }
  243.              WHILE NOT EOF( F1 ) DO
  244.                   BEGIN
  245.                   READ( F1, X );
  246.                   WRITE( F2, X )
  247.                   END
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.         Section 9:   Input/Output
  286.  
  287.         Mystic Pascal  User Manual                                      64
  288.  
  289.  
  290.         9.7  EOLN
  291.  
  292.         EOLN
  293.  
  294.         EOLN( Filvar )
  295.  
  296.              Eoln  is a Boolean function which indicates the  end-of-line 
  297.         condition of a textfile.   If no file variable is specified,  the 
  298.         function assumes the file INPUT.   It is a compiler error if  the 
  299.         file variable is not a textfile.  
  300.  
  301.  
  302.              { Average N numbers from the console }
  303.              SUM := 0;      N := 0;
  304.              WHILE NOT EOLN DO
  305.                   BEGIN
  306.                   READ( X );
  307.                   SUM := SUM + X;
  308.                   N := N + 1
  309.                   END
  310.              WRITELN( 'The average is', SUM DIV N );
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.         Section 9:   Input/Output
  343.  
  344.         Mystic Pascal  User Manual                                      65
  345.  
  346.  
  347.         9.8  GET, PUT and Buffer Variables                  ** 1.6 **
  348.  
  349.              Standard  Pascal supports Input/Output operations which  are 
  350.         more  primitive than Read and Write.   In fact Read and Write are 
  351.         defined in terms of the procedures Get and Put,  which only  move 
  352.         data between a file and its buffer variable.
  353.  
  354.              A  buffer variable is associated with each file.   Its  data 
  355.         type  is  the  same  as the components of  the  file.   A  buffer 
  356.         variable  is accessed by using the file variable's name  followed 
  357.         by an uparrow.
  358.  
  359.              WRITE( F, X )  is equivalent to    F^ := X;  PUT( F )
  360.  
  361.              READ( F, X )   is equivalent to    GET( F );  X := F^
  362.  
  363.  
  364.  
  365.         GET( Filvar )
  366.  
  367.              The  Get  procedure advances the file pointer  to  the  next 
  368.         component of the file.  The current component is available in the 
  369.         file's buffer variable.
  370.  
  371.  
  372.  
  373.         PUT( Filvar )
  374.  
  375.              The  Put procedure appends the value in the buffer  variable 
  376.         to  the file.   The condition EOF( Filvar ) must be true when Put 
  377.         is called.
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.